using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class JobSeekers_jobapplied : System.Web.UI.Page
{
    string connectionString = "";
    string resumeID ="0";
    protected void Page_Load(object sender, EventArgs e)
    {
        connectionString =
                 WebConfigurationManager.ConnectionStrings[
                   "RecruitVietnam"].ConnectionString;
        if (Session.Contents["jsuserid"] == null)
        {
            Response.Redirect("login.aspx");
        }
        resumeID = Request.QueryString.Get("ResumeID");
        if (resumeID == null || resumeID == "") 
            resumeID = "0"; 
        if (!IsPostBack)
            ShowData(resumeID);

    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        ShowData(resumeID);
        GridView1.PageIndex = e.NewPageIndex;
    }
    void ShowData(string resumeID)
    {
        try
        {
            SqlServerProvider sqlServerProvider = 
            new SqlServerProvider(connectionString);
            sqlServerProvider.CommandText = 
            "SPViewAppliedJob";
            sqlServerProvider.ParameterCollection = 
            new string[2] { "@UserID", "@ResumeID" };
            sqlServerProvider.ValueCollection = 
            new string[2] { 
            Convert.ToString(Session.Contents[
            "jsuserid"]), resumeID};
            sqlServerProvider.CommandType = 
            CommandType.StoredProcedure;
            DataTable dataTable = 
            sqlServerProvider.GetDataTable();
            GridViewHelper gridViewHelper = 
            new GridViewHelper();

            gridViewHelper.FillDataTableToGridView(
            dataTable, this.GridView1);
            
            if(ShowDelete(this.GridView1, 7))
                btnDelete.Enabled = true;
            else
                btnDelete.Enabled = false;
             Common common = new Common();
            common.CheckJobExisting(GridView1, 3, 8);
            
        }
        catch(Exception ex)
        {
            Literal1.Text = ex.Message;
        }
    }
     
    
    bool ShowDelete(GridView gridView, int indexColumn)
    {
        bool enableDelete = false;
        foreach (GridViewRow gridViewRow in gridView.Rows)
        {
            CheckBox checkBox = (CheckBox)
            gridViewRow.FindControl("chkJobID");

            if (gridViewRow.Cells[
            indexColumn].Text == "Reject"
            || gridViewRow.Cells[
            indexColumn].Text == "Waiting"
            || gridViewRow.Cells[
            indexColumn].Text == "New")
            {
                 enableDelete =true;                                                
            }
            else
                checkBox.Enabled = false;   
            if (gridViewRow.Cells[5].Text == "0")  
                gridViewRow.Cells[5].Text = "";       
        }
        return enableDelete;
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        string deleteQuery = "";
        foreach (GridViewRow gridViewRow
        in GridView1.Rows)
        {
            CheckBox checkBox = (CheckBox)
            gridViewRow.FindControl("chkJobID");
            if (checkBox.Checked &&
            (gridViewRow.Cells[7].Text == "Reject"
            || gridViewRow.Cells[7].Text == "Waiting"
            || gridViewRow.Cells[7].Text == "New"))
            {
                try
                {
                    deleteQuery += "delete from " +
                    "tblJSAppliedJobs where JobID=" +
                    gridViewRow.Cells[1].Text +
                    " and ResumeID = " +
                    gridViewRow.Cells[2].Text + ";";
                }
                catch (Exception ex)
                {
                    Literal1.Text = ex.Message;
                }
            }

        }
        if (deleteQuery != "")
        {

            RecruitVietnam rcv
                        = new RecruitVietnam();
            rcv.ConnectionString =
            connectionString;
            rcv.DeleteAppliedJob(deleteQuery);
            ShowData(resumeID);
        }
    }
     
}
